//Children
var onKeyDown = function(event) {
	if (event.keyCode === 13) {
		onSearch()
	}
}
var onSearch = function(name, index) {
	if (typeof name === 'string') {
		var keywords = name
		if (typeof index === 'number') {
			console.log(index)
			Based.get('http://cloud-music.pl-fe.cn/search?keywords=' + keywords, hasIndex(index))
		} else {
			Based.get('http://cloud-music.pl-fe.cn/search?keywords=' + keywords, loaded)
		}
	} else {
		var keywords = Dackage.$('.search').value
		Based.get('http://cloud-music.pl-fe.cn/search?keywords=' + keywords, loaded)
	}
	document.body.innerHTML = ''
}
var createMusic = function(url) {
	var audio = document.createElement('audio')
	var source = document.createElement('source')
	audio.setAttribute('controls', 'controls')
	source.setAttribute('src', url)
	source.setAttribute('type', getMIME(url, 'audio/mpeg'))
	audio.appendChild(source)
	return audio
}
function hasIndex(index) {
	var i = index
	return function(element) {
		var data = JSON.parse(element.responseText)
		if (data.code === 200) {
			var songs = data.result.songs, length = songs.length
			function add(id, name) {
				return function() {
					document.body.innerHTML = ''
					document.body.appendChild(addMusic(id, name))
				}
			}
			var div = document.createElement('div')
			div.classList.add('music-lists')
			var name = textFormat('{0} - {1}', songs[i].name, songs[i].artists[0].name)
			var a = document.createElement('button')
			a.addEventListener('click', add(songs[i].id, name))
			a.click()
		}
	}
}
var loaded = function(element) {
	var data = JSON.parse(element.responseText)
	if (data.code === 200) {
		var songs = data.result.songs,
			i, length = songs.length
		function add(id, name) {
			return function() {
				document.body.innerHTML = ''
				document.body.appendChild(addMusic(id, name))
			}
		}
		for(i = 0; i < length; i += 1) {
			var div = document.createElement('div')
			div.classList.add('music-lists')
			var name = textFormat('{0} - {1}', songs[i].name, songs[i].artists[0].name)
			var textNode = document.createTextNode(name)
			div.appendChild(textNode)
			var a = document.createElement('button')
			a.addEventListener('click', add(songs[i].id, name))
			textNode = document.createTextNode('听听')
			a.appendChild(textNode)
			div.appendChild(a)
			document.body.appendChild(div)
		}
	}
}
var addMusic = function(id, musicName) {
	var musicBox = document.createElement('div')
	var p = document.createElement('p')
	musicBox.classList.add('music-box')
	var hidden = document.createElement('div')
	hidden.classList.add('hidden')
	musicBox.appendChild(hidden)
	var url = 'http://music.163.com/song/media/outer/url?id=' + id
	var audio = createMusic(url)
	Based.get('http://cloud-music.pl-fe.cn/lyric?id=' + id, IRC2object)
	hidden.appendChild(audio)

	var info = document.createElement('div')
	info.classList.add('info')
	var name = document.createElement('span')
	name.classList.add('name')
	var textNode = document.createTextNode(musicName)
	name.appendChild(textNode)
	info.appendChild(name)
	musicBox.appendChild(info)

	var progress = document.createElement('div')
	progress.classList.add('progress')
	var current = document.createElement('div')
	current.classList.add('current')
	progress.appendChild(current)
	var bull = document.createElement('p')
	bull.classList.add('bull')
	current.appendChild(bull)
	musicBox.appendChild(progress)

	var controls = document.createElement('div')
	controls.classList.add('controls')
	var time = document.createElement('span')
	time.classList.add('time')
	var play = document.createElement('span')
	play.classList.add('play')
	textNode = document.createTextNode('play')
	play.appendChild(textNode)
	var onClick = function(media) {
		return function() {
			media.play()
		}
	}
	play.addEventListener('click', onClick(audio))
	controls.appendChild(time)
	controls.appendChild(play)
	musicBox.appendChild(controls)

	p.classList.add('word')
	musicBox.appendChild(p)
	return musicBox
}
var IRC2object = function(element) {
	var data = JSON.parse(element.responseText)
	var audios = Dackage.$All('.music-box audio')
	var irc = {}
	irc = IRCparse(data.lrc.lyric)
	audios[audios.length - 1].addEventListener('timeupdate', getCurrentTime(irc), false)
	if (data.tlyric && data.tlyric.lyric) {
		irc = IRCparse(data.tlyric.lyric)
		console.log(irc)
		var p = document.createElement('p')
		p.classList.add('word')
		var div = Dackage.$All('.music-box')
		div = div[div.length - 1]
		div.appendChild(p)
		audios[audios.length - 1].addEventListener('timeupdate', getCurrentTime(irc), false)
	}
}
var getCurrentTime = function(irc) {
	var p = Dackage.$All('.music-box .word'), lineNo = 0
	p = p[p.length - 1]
	var audios = Dackage.$All('.music-box audio')
	var duration = audios[audios.length - 1].duration
	var current = Dackage.$All('.music-box .progress .current')
	current = current[current.length - 1]
	var musicTime = Dackage.$All('.music-box .controls .time')
	musicTime = musicTime[musicTime.length - 1]
	console.log(p)
	return function(element) {
		element = element.target
		var currentTime = element.currentTime
		if (currentTime <= irc['first']) {
			lineNo = 0
			p.innerText = ''
		}
		var progress = (currentTime / duration * 100) + '%'
		current.style.width = progress
		var min = parseInt((currentTime / 59).toString())
		var sec = currentTime.toFixed() - 59 * min
		musicTime.innerText = textFormat('{0} : {1}', min, sec)
		if (irc[lineNo]&&irc[lineNo].time <= currentTime) {
			p.innerText = (irc[lineNo].string)? irc[lineNo].string: p.innerText
			lineNo += 1
		}
	}
}
var onRandom = function() {
	return 0
}
//Main
function main() {
	if (Based.isSet('name')) {
		var name = Based.greet('name')
		console.log(name)
		if (Based.isSet('index')) {
			var index = Based.greet('index')
			onSearch(name, + index)
		} else {
			onSearch(name)
		}
	}
	Dackage.$('.search').addEventListener('search', onSearch)
	Dackage.$('.search').addEventListener('keydown', onKeyDown)
	Dackage.$All('.search')[1].addEventListener('click', onSearch)
	Dackage.$('.random').addEventListener('click', onRandom)
	return 0
}

window.addEventListener('load', main)